﻿create PROCEDURE Sync.SP_Images_SelectChanges
@LastRowVersion rowversion,
@FilterdColumn UNIQUEIDENTIFIER
AS 
BEGIN 
	SELECT 
        t.[ImageID],
		g.[ImageData],g.[ImageTypeID],g.[Label],
		t.IsDeleted,
		t.LastUpdatedDate
	FROM [dbo].[Images] g RIGHT JOIN Sync.Images_Tracking t  
    ON t.ImageID = g.ImageID 
	WHERE (@LastRowVersion IS NULL OR t.Version > @LastRowVersion)
	and
		(g.ImageID in (select lfsatt.Attachment from [LoanFeasabilityStudyAttachments] lfsatt
						join [LoanFeasabilityStudy] fstudy
						on lfsatt.LoanFeasabilityStudyID = fstudy.LoanFeasabilityStudyID
						join [CustomerLoans] cl1
						on fstudy.LoanID = cl1.LoanID
						where cl1.CommitteeID = @FilterdColumn)
		or g.ImageID in (select lsratt.Attachment from [LoanSocialResearchAttachments] lsratt
						join [LoanSocialResearch] sresearch
						on lsratt.LoanSocialResearchID = sresearch.LoanSocialResearchID
						join [CustomerLoans] cl2
						on sresearch.LoanID = cl2.LoanID
						where cl2.CommitteeID = @FilterdColumn)
		or g.ImageID in (select batt.Attachment from [BoardAttachments] batt
						join [Board] b
						on b.BoardID = batt.BoardID
						where b.CommitteeID = @FilterdColumn))
    END














